package main

import (
	"context"
	"e-commerce/services/payment-service/otel"
	"log"

	mygin "e-commerce/services/payment-service/api/gin"

	"github.com/gin-gonic/gin"
	"go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin"
)

func main() {
	shutdown, err := otel.InitTracer("payment-service", "services/payment-service/config/devconfig.yaml")
	if err != nil {
		log.Fatalf("Failed to initialize tracer: %v", err)
	}
	defer func() {
		if err := shutdown(context.Background()); err != nil {
			log.Printf("Failed to shutdown tracer: %v", err)
		}
	}()
	paymentHandler, err := mygin.NewPaymentHandler("services/payment-service/config/devconfig.yaml")
	if err != nil {
		log.Fatalf("Failed to create payment handler: %v", err)
	}
	r := gin.Default()
	r.Use(otelgin.Middleware("payment-service"))
	r.Use(paymentHandler.Middleware())
	log.Println("paymentHandler: ", paymentHandler)
	r.POST("/payment/success/:orderID", paymentHandler.MarkOrderPaidSuccess)
	r.POST("/payment/failed/:orderID", paymentHandler.MarkOrderPaidFailed)
	go r.Run(":8085")

	select {}
}
